package com.shhic.hhplt.server.facade;

import javax.annotation.Resource;

import com.shhic.hhplt.server.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shhic.hhplt.server.biz.IWorkbayStateBuffer;
import com.shhic.hhplt.server.model.CommonRtnValue;
import com.shhic.hhplt.server.model.NoPayload;
import com.shhic.hhplt.server.model.User;
import com.shhic.hhplt.server.model.WorkbayClient;

/**
 * 工位管理门面
 * @author 张文硕
 *
 */
@Controller
public class WorkbayManageController {
	@Autowired
	IUserService userService;
	@Resource IWorkbayStateBuffer workbayStateBuffer;
	
	@RequestMapping(value = "/login.do", method = RequestMethod.POST)
	@ResponseBody
	public CommonRtnValue<User> login(WorkbayClient client){
		User user = userService.getUser(client.getUsername(),client.getPassword());
		if(user == null){
			return new CommonRtnValue<User>(1,"用户名或密码错误");
		}
		userService.registerProductAndSuite(client.getProduct(), client.getTestSuite());
		workbayStateBuffer.notifyWorkbayLogin(client.getWorkbay());
		
		return new CommonRtnValue<User>(user);
	}
	
	@RequestMapping(value = "/modifyPassword.do", method = RequestMethod.POST)
	@ResponseBody
	public CommonRtnValue<NoPayload> modifyPassword(@RequestParam String username,@RequestParam String oldpassword,@RequestParam String newpassword){
		User user = userService.getUser(username,oldpassword);
		if(user == null){
			return new CommonRtnValue<NoPayload>(1,"用户名或原密码错误");
		}
		userService.updateUserPassword(username,newpassword);
		return new CommonRtnValue<NoPayload>();
	}
}
